<template>
  <Card>
    <Form ref="searchForm" :label-width="90" :model="searchForm" inline>
      <Form-item label="日期范围：" prop="date">
        <DatePicker
          v-model="searchForm.date"
          placeholder="请选择日期范围"
          placement="bottom-start"
          style="width: 200px"
          type="daterange"
        ></DatePicker>
      </Form-item>
      <Form-item label="车牌号：" prop="license_plate_num">
        <Input
          v-model="searchForm.license_plate_num"
          clearable
          placeholder="支持模糊搜索"
          style="width: 200px"
          type="text"
        />
      </Form-item>
      <Form-item label="车辆线路：" prop="line">
        <Select v-model="searchForm.line" clearable placeholder="选择车牌号" style="width:200px;">
          <Option value="-1">全部</Option>
          <Option v-for="item in lines" :key="item.value" :value="item.value">{{ item.label }}</Option>
        </Select>
      </Form-item>
      <Form-item style="margin-left:-90px;">
        <Button icon="md-search" type="primary" @click="handleSearch">搜索</Button>
        <Button @click="handleReset">重置</Button>
      </Form-item>
    </Form>
    <div class="btns" style="margin-bottom:10px;">
      <upload-file ref="uploadCardFile" button-name="导入" @on-change="uploadData($event)"></upload-file>
    </div>
    <Table
      :columns="column"
      :data="data"
      :loading="loading"
      border
      size="small"
      stripe></Table>
    <Row class="page" justify="end" type="flex">
      <Page
        :current="searchForm.pageNumber"
        :page-size="searchForm.pageSize"
        :total="total"
        show-total
        @on-change="changePage"
      />
    </Row>
    <Modal v-model="modal" :closable="false" :mask-closable="false" width="360">
      <p slot="header" style="color:#2d8cf0;text-align:center">
        <Icon type="ios-information-circle"></Icon>
        <span>文件上传成功，正在导入数据</span>
      </p>
      <div style="text-align:center">
        <Button :loading="importLoading" long size="large" type="error">导入中...</Button>
      </div>
      <div slot="footer"></div>
    </Modal>
  </Card>
</template>
<script>
import UploadFile from '../my-components/upload-file'
import {importMileage, mileageList} from '../../api/mileage'

export default {
  components: {
    UploadFile
  },
  data() {
    return {
      searchForm: {
        line: '-1',
        license_plate_num: '',
        date: [null, null],
        pageNumber: 1, // 当前页数
        pageSize: 10 // 页面大小
      },
      loading: false,
      data: [],
      modal: false,
      importLoading: false,
      total: 0,
      column: [
        {type: 'index', width: 60, align: 'center', fixed: 'left'},
        {
          title: '时间',
          key: 'date',
          align: 'center',
          width: 120,
          fixed: 'left'
        },
        {
          title: '线路',
          key: 'line',
          align: 'center',
          width: 100,
          fixed: 'left'
        },
        {
          title: '车牌号',
          key: 'license_plate_num',
          align: 'center',
          width: 150,
          fixed: 'left'
        },
        {title: '自编号', key: 'self_numbering', align: 'center'},
        {title: '计划趟次', key: 'plan_times', align: 'right'},
        {title: '实际趟次', key: 'actual_times', align: 'right'},
        {title: '掉趟', key: 'drop_off', align: 'right'},
        {title: '出车率', key: 'departure_rate', align: 'right'},
        {title: '运营里程', key: 'operating_mileage', align: 'right'},
        {title: '非运营里程', key: 'non-operating_mileage', align: 'right'},
        {title: 'GPS总里程', key: 'gps_mileage', align: 'right'},
        {title: '计划里程', key: 'plan_mileage', align: 'right'},
        {title: '额定里程', key: 'rated_mileage', align: 'right'}
      ],
      lines: []
    }
  },
  mounted() {
    this.init()
    this.getLinesArray()
  },
  methods: {
    async getLinesArray() {
      this.lines = await this.getLines()
    },
    changePage(v) {
      this.searchForm.pageNumber = v
      this.getMileageList()
    },
    getMileageList() {
      this.loading = true
      mileageList(this.searchForm).then(res => {
        this.loading = false
        if (res.data) {
          this.data = res.data.gasRecord
          this.total = res.data.count
        }
      })
    },
    handleReset() {
      this.$refs.searchForm.resetFields()
      this.searchForm.pageNumber = 1
      this.searchForm.pageSize = 10
      this.getMileageList()
    },
    handleSearch() {
      this.searchForm.pageNumber = 1
      this.searchForm.pageSize = 10
      this.getMileageList()
    },
    init() {
      this.getMileageList()
    },
    uploadData(event) {
      this.modal = true
      this.importLoading = true
      importMileage({url: event.url}).then(res => {
        if (res.data) {
          this.$Message.success(res.message)
          this.getMileageList()
        } else {
          this.$Message.error(res.message)
        }
        this.modal = false
        this.importLoading = false
      })
    }
  }
}
</script>
